package com.aoxin.controller.client;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.course.SlfChildren;
import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.dto.UserAndChildrenDto;
import com.aoxin.model.opm.SlfUser;
import com.aoxin.service.inter.course.webCourse.ChildrenService;
import com.aoxin.service.inter.opm.SchoolService;
import com.aoxin.service.inter.opm.UserService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.CookieUtil;
import com.aoxin.util.ErrorUtil;
import com.aoxin.util.RedisUtil;
import com.aoxin.util.UUID;

/**
 * 注册Controller
 *
 * @author Melody
 * @date 2018年4月2日 下午5:48:09
 */
@Controller
@ResponseBody
@RequestMapping("/web/register")
public class RegisterController {
    private Logger logger = LoggerFactory.getLogger(RegisterController.class);
    @Autowired
    private SchoolService schoolService;

    @Autowired
    private UserService userService;

    @Autowired
    private ChildrenService childrenService;

    /**
     * 查询学校下拉框
     *
     * @param ajaxRes
     * @return
     */
    @RequestMapping("/getSchoolList")
    public AjaxRes getSchoolList(AjaxRes ajaxRes) {
        try {
            List<SelectDto> selectDto = this.schoolService.getSchoolSelectList();
            ajaxRes.setSucceed(selectDto, Const.DATA_SUCCEED);
        } catch (Exception e) {
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    /**
     * 用户注册 并登陆
     * @param userAndChildrenDto 用户信息
     * @param ajaxRes
     * @return
     */
    @RequestMapping("/userRegister")
    public AjaxRes UserRegister(UserAndChildrenDto userAndChildrenDto, HttpServletRequest request, AjaxRes ajaxRes) {
        try {
            SlfUser user = this.userService.insertUser(userAndChildrenDto);
            if (null != user) {
                String token = UUID.getUUID();
                user.setToken(token);
                HttpSession session = request.getSession();
                String oldtoken = (String) RedisUtil.get(user.getPhone() + user.getUserId());
                if (null != oldtoken) {
                    RedisUtil.del(oldtoken);
                    RedisUtil.del(user.getPhone() + user.getUserId());
                }
                session.setAttribute("userSession", user);
                RedisUtil.set(user.getPhone() + user.getUserId(), token);
                RedisUtil.set(token, user);
                List<SlfChildren> childrens = this.childrenService.getChildrenInfo(user.getUserId());
                SlfChildren oldslfChildren = (SlfChildren) RedisUtil.get(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
                if (null != oldslfChildren) {
                    RedisUtil.del(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE);
                }
                // 默认选中最新添加的孩子
                if (childrens.size() > 0) {
                    RedisUtil.set(user.getPhone() + user.getUserId() + ChildrenService.REDIS_CHILDREN_CODE, childrens.get(0));
                }
            }
            CookieUtil.saveCookie("slfCookie", user.getUserId().toString());
            if (user.getAddrDelFlag() == 0) {
                ajaxRes.setSucceedMsg(ErrorUtil.REGISTERED_SUCCESS);
            } else if (user.getAddrDelFlag() == 1) {
                ajaxRes.setFailMsg(ErrorUtil.REGISTERED_FAILURE);
            } else if (user.getAddrDelFlag() == 2) {
                ajaxRes.setSucceedMsg(ErrorUtil.REGISTERED_ADDRDEL_SUCCESS);
            } else {
                logger.info(ErrorUtil.ADDR_ERROR);
            }
        } catch (Exception e) {
            ajaxRes.setFailMsg(e.getMessage());
        }
        return ajaxRes;
    }

    @RequestMapping("/scavenging")
    public void scavenging(HttpServletRequest request, HttpServletResponse response) {
        String header = request.getHeader("UserAgent");
        System.err.println(header);
        //todo
        if (header.contains("MicroMessenger")) {
            //微信
        } else if (header.contains("ApliPayClient")) {
            //支付宝
        } else {
            //浏览器
        }

        request.getParameterMap();
        try {
            response.sendRedirect("/slf/register.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
